<?php
function getImagePath( $image_id ) {
	$id = $image_id;
	if ((strlen($id))>8){
		$loop_flag = true;$i = 0;
		while($loop_flag){
			if(substr($image_id,$i) * 1) {
				$loop_flag = false;
			} else {
				$i++;
			}
			if($i>8) $loop_flag = false;
		}
		$prefix = strtolower(substr($id, 0, $i));
		$id= substr($id, $i);
	} else {
	$prefix="";
	}
	$destPath  = $prefix . "/";
	$destPath .= (int) ($id / 1000000) . "/";
	$destPath .= (int) ( ($id % 1000000) / 10000) . "/";
	$destPath .= (int) ( ($id % 10000) / 100) . "/";
	$destPath .= (int) ( $id % 100 ) . "/";
	return( $destPath );
}

function getCardName($id) {
	$prefix = 'card';
	return $prefix . str_pad($id,6,'0',STR_PAD_LEFT);
}

function mkdir_recursive( $pathname ) {
        is_dir(dirname($pathname)) || mkdir_recursive(dirname($pathname));
        return is_dir($pathname) || @mkdir($pathname, 0775);
}

function buildWhere($filter = '') {
	$where = " 0 = 0 ";
	if (is_array($filter)) {
	for ($i=0;$i < count($filter);$i++){
		switch($filter[$i]['data']['type']){

		case 'string' : 
			switch ($filter[$i]['data']['comparison']) {
				case 'eq': $qs .= " AND ".$filter[$i]['field']." = '".$filter[$i]['data']['value']."'";
					break;
				case 'ne': $qs .= " AND ".$filter[$i]['field']." != '".$filter[$i]['data']['value']."'";
					break;
				case 'list':
					if(strstr($filter[$i]['data']['value'],',')){
						$fi = explode(',',$filter[$i]['data']['value']);
						for ($q=0;$q<count($fi);$q++){
							$fi[$q] = "'".$fi[$q]."'";
						}
						$filter[$i]['data']['value'] = implode(',',$fi);
						$qs .= " AND ".$filter[$i]['field']." IN (".$filter[$i]['data']['value'].")";
					}else{
						$qs .= " AND ".$filter[$i]['field']." = '".$filter[$i]['data']['value']."'";
					}
					break;
				case 'slk': $qs .= " AND ".$filter[$i]['field']." LIKE '%".$filter[$i]['data']['value']."'";
					break;
				case 'elk': $qs .= " AND ".$filter[$i]['field']." LIKE '".$filter[$i]['data']['value']."%'";
					break;
				case 'lk': $qs .= " AND ".$filter[$i]['field']." LIKE '%".$filter[$i]['data']['value']."%'";
					break;
			}
			break;
		case 'numeric':
			switch ($filter[$i]['data']['comparison']) {
				case 'eq' : $qs .= " AND ".$filter[$i]['field']." = ".$filter[$i]['data']['value'];
					break;
				case 'ne' : $qs .= " AND ".$filter[$i]['field']." != ".$filter[$i]['data']['value'];
					break;
				case 'lt' : $qs .= " AND ".$filter[$i]['field']." < ".$filter[$i]['data']['value'];
					break;
				case 'gt' : $qs .= " AND ".$filter[$i]['field']." > ".$filter[$i]['data']['value'];
					break;
				case 'lte' : $qs .= " AND ".$filter[$i]['field']." <= ".$filter[$i]['data']['value'];
					break;
				case 'gte' : $qs .= " AND ".$filter[$i]['field']." >= ".$filter[$i]['data']['value'];
					break;
				case 'list':
					if(strstr($filter[$i]['data']['value'],',')){
						$qs .= " AND ".$filter[$i]['field']." IN (".$filter[$i]['data']['value'].")";
					}else{
						$qs .= " AND ".$filter[$i]['field']." = ".$filter[$i]['data']['value'];
					}
					break;
			}
			break;
		case 'boolean' : $qs .= " AND ".$filter[$i]['field']." = ".($filter[$i]['data']['value']);
			break;
		case 'date' :
			switch ($filter[$i]['data']['comparison']) {
				case 'eq' : $qs .= " AND ".$filter[$i]['field']." = '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'ne' : $qs .= " AND ".$filter[$i]['field']." != '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'lt' : $qs .= " AND ".$filter[$i]['field']." < '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'gt' : $qs .= " AND ".$filter[$i]['field']." > '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'lte' : $qs .= " AND ".$filter[$i]['field']." <= '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'gte' : $qs .= " AND ".$filter[$i]['field']." >= '".date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
			}
			break;
		case 'list'   :
			if (strstr($filter[$i]['data']['value'],',')){
			$fi = explode(',',$filter[$i]['data']['value']);
			for ($q=0;$q<count($fi);$q++){
				$fi[$q] = "'".$fi[$q]."'";
			}
			$filter[$i]['data']['value'] = implode(',',$fi);
			$qs .= " AND ".$filter[$i]['field']." IN (".$filter[$i]['data']['value'].")";
			}else{
			$qs .= " AND ".$filter[$i]['field']." = '".$filter[$i]['data']['value']."'";
			}
			break;
		} # switch
	} # for
	$where .= $qs;
	} # if
        return $where;
}

?>